package cn.tedu._05mvcboo02.cotroller;

import cn.tedu._05mvcboo02.mapper.UserMapper;
import cn.tedu._05mvcboo02.pojo.dto.UserDTO;
import cn.tedu._05mvcboo02.pojo.entity.User;
import cn.tedu._05mvcboo02.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/users/")
//@Controller
public class UserController {

    @Autowired
    private UserMapper userMapper;
//    @ResponseBody 允许方法以返回值的方式返回给用户数据
//    @RequestMapping("insert")
    @PostMapping("insert")
    public String insert(UserDTO userDTO){
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return "添加成功";
    }

    @RequestMapping("userlist")
//    @ResponseBody
    public List<UserListVO> userList(){
        return userMapper.userList();

    }

    @RequestMapping("delete")
//    @ResponseBody
    public  String delete(int id){
        userMapper.delete(id);
        return "删除成功";
    }

    @RequestMapping("v1/users/update")
//    @ResponseBody
    public String update(User user){
        userMapper.update(user);
        return "修改成功";
    }

}
